/** 
* @author: л����
* @date��2017��7��5�� ����1:44:17
* @version 1.0 
*/
package dao;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

import bean.Right;
import bean.User;
import bean.model.PageModel;
import util.PageUtil;

public class UserDao {
	private Session session = null;
	private Transaction transaction = null;

	public boolean insertUser(final User user) {
		try {
			session = DaoUtil.getSession();
			transaction = session.beginTransaction();
			session.save(user);
			transaction.commit();
		} catch (HibernateException e) {
			if (transaction != null) {
				transaction.rollback();
			}
			return false;
		}
		return true;
	}

	public boolean deleteUser(final User user) {
		try {
			session = DaoUtil.getSession();
			transaction = session.beginTransaction();
			session.delete(user);
			transaction.commit();
		} catch (HibernateException e) {
			if (transaction != null) {
				transaction.rollback();
			}
			return false;
		}
		return true;
	}

	public boolean deleteUser(final int user_id) {
		try {
			session = DaoUtil.getSession();
			transaction = session.beginTransaction();
			// ����load��ѯ�����ڵ�����,hibernate���׳�object not found exception
			User user = (User) session.load(User.class, user_id);

			// ɾ�����еļ�¼.
			// ɾ��,�����ô��ַ�ʽɾ��,�ȼ�����ɾ��.
			session.delete(user);
			transaction.commit();
		} catch (HibernateException e) {
			if (transaction != null) {
				transaction.rollback();
			}
			return false;
		}
		return true;
	}

	public User getUser(final int user_id) {
		User result = new User();
		try {
			session = DaoUtil.getSession();
			transaction = session.beginTransaction();
			// ����load��ѯ�����ڵ�����,hibernate���׳�object not found exception
			result = (User) session.load(User.class, user_id);
			transaction.commit();
		} catch (HibernateException e) {
			if (transaction != null) {
				transaction.rollback();
			}
		}
		return result;
	}

	public User getUser(final String user_name) {
		User result = new User();
		try {
			session = DaoUtil.getSession();
			transaction = session.beginTransaction();
			String hql = "from User user where user.userName=:name";
			@SuppressWarnings("unchecked")
			final Query<User> query = session.createQuery(hql);
			query.setParameter("name", user_name);
			final List<User> list = query.list();
			if (list.size() >= 1) {
				result = list.get(0);
			}
			transaction.commit();
		} catch (HibernateException e) {
			if (transaction != null) {
				transaction.rollback();
			}
		}
		return result;
	}

	public boolean updateUser(final User user) {
		try {
			session = DaoUtil.getSession();
			transaction = session.beginTransaction();
			// ����load��ѯ�����ڵ�����,hibernate���׳�object not found exception
			User original = (User) session.load(User.class, user.getUserId());
			original.setBirthday(user.getBirthday());
			original.setImg(user.getImg());
			original.setPhoneNumber(user.getPhoneNumber());
			original.setSex(user.getSex());
			original.setTruename(user.getTruename());
			session.update(original);
			transaction.commit();
		} catch (HibernateException e) {
			if (transaction != null) {
				transaction.rollback();
			}
			return false;
		}
		return true;
	}

	public int getUserNum() {
		session = DaoUtil.getSession();
		transaction = session.beginTransaction();
		String hql = "select count(user.userId) as count from User user";
		final Query createQuery = session.createQuery(hql);
		final List list = createQuery.list();
		int count = 0;
		for (Object object : list) {
			count = ((Number) object).intValue();
		}
		return count;
	}

	public List<User> queryByPage(final PageModel page) {
		session = DaoUtil.getSession();
		transaction = session.beginTransaction();
		String hql = "from User";
		@SuppressWarnings("unchecked")
		Query<User> query = session.createQuery(hql);
		// 设置每页显示多少个，设置多大结果。
		query.setMaxResults(page.getEveryPage());
		// 设置起点
		query.setFirstResult(page.getBeginIndex());
		return query.list();
	}

	public static void main(String[] args) {
		final PageModel createPage = PageUtil.createPage(15, 20, 1);
		UserDao dao = new UserDao();
		final User user = dao.getUser("bdxz");
		final Set rights = user.getRights();

		final Iterator iterator = rights.iterator();
		while (iterator.hasNext()) {
			Right right = (Right) iterator.next();
			System.out.println(right.getRightName());
		}
	}
}
